/*
 * core/RPCCallback.java
 *
 * Callback to overload to handle RPC response.
 */

package net.kernub.client.core;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

import net.kernub.client.core.RPCResponse;

public class RPCCallback<Ty>
{
	public RPCCallback() {}

	// onError(): to overload
	public void onError( String error ) 
	{
		final String msg = "RPCCallback.onError: unhandled error: " + error;
		GWT.log( msg, null );
		Window.alert( msg );
	}

	// onCallback(): to overload
	public void onCallback( Ty result ){}

	// callFromJSON(): used by RPC
	public void callFromJSON( String json )
	{
		RPCResponse<Ty> response = this.fromJSON( json );

		if( response.hasError() )
		{
			this.onError( response.getError() );
		}
		else
		{
			this.onCallback( response.getObject() );
		}
	}

	private final native RPCResponse<Ty> fromJSON( String json ) /*-{ return eval(json)[0]; }-*/;
};


